package cc.orange.mainView;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import cc.orange.R;
import cc.orange.base.BaseActivity;
import cc.orange.databinding.ActivityFateShakeBinding;
import cc.orange.entity.BaseEntity;
import cc.orange.entity.HightEntity;
import cc.orange.entity.IsLoginsEntity;
import cc.orange.entity.LoginPswPostEntity;
import cc.orange.entity.LoginsEntity;
import cc.orange.entity.MsgLoginUI3Entity;
import cc.orange.entity.NoteExtrEntity;
import cc.orange.entity.SuerInfosEntity;
import cc.orange.http.ApiService;
import cc.orange.utils.ZXToastUtil;
import cc.orange.utils.popup.PopupShFateView;
import cc.orange.utils.popup.PopupShakeInfo;
import cc.orange.utils.popup.PopupShakeView;
import demo.smart.access.xutlis.util.ZXSharedPrefUtil;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
//import com.tendcloud.tenddata.TalkingDataGender;
//import com.tendcloud.tenddata.TalkingDataProfile;
//import com.tendcloud.tenddata.TalkingDataProfileType;
//import com.tendcloud.tenddata.TalkingDataSDK;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class FateShakeActivity extends BaseActivity implements View.OnClickListener{

    private ActivityFateShakeBinding binding;
    private SuerInfosEntity entity;
    private  NoteExtrEntity entity_note ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_fate_shake);
        initViews();
        getSuserInfo();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initViews() {
        binding.fsBack.setOnClickListener(this);

        binding.fsRel3.setOnClickListener(this);
        binding.fsRel4.setOnClickListener(this);
        binding.fsRel5.setOnClickListener(this);
        binding.fsRel6.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fs_back:
                finish();
                break;
            case R.id.fs_rel3://女 放
                if(null != entity && entity.getData().getSex() == 1){
                    ZXToastUtil.showToast("男生只能放男生纸条");
                }else{
//                    ZXSharedPrefUtil ss = new ZXSharedPrefUtil();
//                    NoteAdd(ss.getString("url_age"),ss.getString("url_addres"),ss.getString("url_sex"));
                    PopupShakeView();
                }
                break;
            case R.id.fs_rel4://女 抽
                if(null != entity && entity.getData().getSex() == 0){
                    ZXToastUtil.showToast("女生只能抽男生纸条");
                }else{
                    ZXSharedPrefUtil ss = new ZXSharedPrefUtil();
                    NoteExtract(ss.getString("url_age"),ss.getString("url_addres"),ss.getString("url_sex"));
                }
                break;
            case R.id.fs_rel5://男  放
                if(null != entity && entity.getData().getSex() == 0) {
                    ZXToastUtil.showToast("女生只能放女生纸条");
                }else{
//                    PopupShakeView(2);
                    PopupShakeView();
                }
                break;
            case R.id.fs_rel6://男 抽
                if(null != entity && entity.getData().getSex() == 1){
                    ZXToastUtil.showToast("男生只能抽女生纸条");
                }else{
                    ZXSharedPrefUtil ss = new ZXSharedPrefUtil();
                    NoteExtract(ss.getString("url_age"),ss.getString("url_addres"),ss.getString("url_sex"));

                }
                break;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getHight(HightEntity entity) {//改变布局
        if(entity.getHight() > 0){
            RelativeLayout.LayoutParams pp = new RelativeLayout.
                    LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    entity.getHight());
            binding.fsTitle.setLayoutParams(pp);
        }
    }


    private void NoteAdd(String age,String city,String sex,String content){

        String s = "{\"age\":\""+age+"\",\"city\":\""+city+"\",\"sex\":\""+sex+"\",\"content\":\""+content+"\"}";
        RequestBody requestBody1 = RequestBody.create(MediaType.parse("application/json"), s.toString());

        RetrofitFactory.create(ApiService.class)
                .NoteAdd(getTokens(),requestBody1)
                .enqueue(new DefaultCallback<BaseEntity>() {
                    @Override
                    public void onStart(Call<BaseEntity> call) {
                        showLoading_base(FateShakeActivity.this);
                    }

                    @Override
                    public void onError(Call<BaseEntity> call, HttpError error) {
                        ZXToastUtil.showToast("出错了，请稍后重试");
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            ZXToastUtil.showToast("已投入纸条");
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }


    private void NoteExtract(String age,String city,String sex){

        String s = "{\"age\":\""+age+"\",\"city\":\""+city+"\",\"sex\":\""+(sex.equals("1") ? "0":"1")+"\"}";
        RequestBody requestBody1 = RequestBody.create(MediaType.parse("application/json"), s.toString());

        RetrofitFactory.create(ApiService.class)
                .NoteExtract(getTokens(),requestBody1)
                .enqueue(new DefaultCallback<NoteExtrEntity>() {
                    @Override
                    public void onStart(Call<NoteExtrEntity> call) {
                        showLoading_base(FateShakeActivity.this);
                    }

                    @Override
                    public void onError(Call<NoteExtrEntity> call, HttpError error) {
                        cancelLoading();
                        ZXToastUtil.showToast("出错了，请稍后重试");
                    }

                    @Override
                    public void onSuccess(Call<NoteExtrEntity> call, NoteExtrEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            entity_note = baseEntity;
                            showPopup(baseEntity);
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }

    private void showPopup(NoteExtrEntity baseEntity) {
        PopupShakeInfo.showPopupwindow(this, baseEntity, binding.fsBack, new PopupShakeInfo.PopuStatus() {
            @Override
            public void popupShow() {

            }

            @Override
            public void popupDismissCancle(String se1) {
                reply(se1);
            }
        });
    }

    private void getSuserInfo(){
        RetrofitFactory.create(ApiService.class)
                .getSuserInfo(getTokens())
                .enqueue(new DefaultCallback<SuerInfosEntity>() {
                    @Override
                    public void onStart(Call<SuerInfosEntity> call) {
                        showLoading_base(FateShakeActivity.this);
                    }

                    @Override
                    public void onError(Call<SuerInfosEntity> call, HttpError error) {
                        cancelLoading();
                        EventBus.getDefault().post(new MsgLoginUI3Entity(true));
                    }

                    @Override
                    public void onSuccess(Call<SuerInfosEntity> call, SuerInfosEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            entity = baseEntity;
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }


    private void PopupShakeView() {
        PopupShFateView.showPopupwindow(this, binding.fsBack, new PopupShFateView.PopuStatus() {
            @Override
            public void popupShow() {

            }

            @Override
            public void popupDismissCancle(String se1) {
                ZXSharedPrefUtil ss = new ZXSharedPrefUtil();
                NoteAdd(ss.getString("url_age"),ss.getString("url_addres"),ss.getString("url_sex"),se1);
            }
        });
    }

    private void reply(String id){
        String s = "{\"id\":\""+id+"\"}";
        RequestBody requestBody1 = RequestBody.create(MediaType.parse("application/json"), s.toString());

        RetrofitFactory.create(ApiService.class)
                .reply(getTokens(),requestBody1)
                .enqueue(new DefaultCallback<BaseEntity>() {
                    @Override
                    public void onStart(Call<BaseEntity> call) {
                        showLoading_base(FateShakeActivity.this);
                    }

                    @Override
                    public void onError(Call<BaseEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            Intent intent = new Intent(FateShakeActivity.this,ConversationListActivity.class);
                            intent.putExtra("uid",""+entity_note.getData().getId());
                            intent.putExtra("u_name",
                                    ""+(TextUtils.isEmpty(entity_note.getData().getRemarkName()) ?
                                            entity_note.getData().getName() : entity_note.getData().getRemarkName()));
                            intent.putExtra("u_spare",""+entity_note.getData().getSpareStatus());
                            intent.putExtra("type_t", "0");
                            startActivity(intent);
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }

}